home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / Autoscroller.java < prev    next >
Text File  |  1998-06-30  |  3KB  |  109 lines

  1. /*
  2.  * @(#)Autoscroller.java    1.5 98/01/30
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25.  
  26. import java.io.Serializable;
  27. import java.io.ObjectOutputStream;
  28. import java.io.ObjectInputStream;
  29. import java.io.IOException;
  30.  
  31.  
  32. /**
  33.  * @version 1.5 01/30/98
  34.  * @author Dave Moore
  35.  */
  36.  
  37. class Autoscroller extends MouseAdapter implements Serializable
  38. {
  39.     transient MouseEvent event;
  40.     transient Timer timer;
  41.     JComponent component;
  42.  
  43.  
  44.     Autoscroller(JComponent c) {
  45.     if (c == null) {
  46.         throw new IllegalArgumentException("component must be non null");
  47.     }
  48.     component = c;
  49.     timer = new Timer(100, new AutoScrollTimerAction());
  50.     component.addMouseListener(this);
  51.     }
  52.  
  53.     class AutoScrollTimerAction implements ActionListener {
  54.     public void actionPerformed(ActionEvent x) {
  55.         if(!component.isShowing() || (event == null)) {
  56.         stop();
  57.         return;
  58.         }
  59.         Point screenLocation = component.getLocationOnScreen();
  60.         MouseEvent e = new MouseEvent(component, event.getID(),
  61.                       event.getWhen(), event.getModifiers(),
  62.                       event.getX() - screenLocation.x,
  63.                       event.getY() - screenLocation.y,
  64.                       event.getClickCount(), event.isPopupTrigger());
  65.         component.superProcessMouseMotionEvent(e);
  66.     }
  67.     }
  68.  
  69.     void stop() {
  70.     timer.stop();
  71.     event = null;
  72.     }
  73.  
  74.     public void mouseReleased(MouseEvent e) {
  75.     stop();
  76.     }
  77.  
  78.     public void mouseDragged(MouseEvent e) {
  79.     Rectangle visibleRect = component.getVisibleRect();
  80.     boolean contains = visibleRect.contains(e.getX(), e.getY());
  81.  
  82.     if (contains) {
  83.         if (timer.isRunning()) {
  84.         stop();
  85.         }
  86.     } else {
  87.         Point screenLocation = component.getLocationOnScreen();
  88.  
  89.         event = new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(),
  90.                    e.getX() + screenLocation.x,
  91.                    e.getY() + screenLocation.y,
  92.                    e.getClickCount(), e.isPopupTrigger());
  93.         if (!timer.isRunning()) {
  94.         timer.start();
  95.         }
  96.     }
  97.     }
  98.  
  99.     private void writeObject(ObjectOutputStream s) throws IOException {
  100.     s.defaultWriteObject();
  101.     }
  102.  
  103.     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException 
  104.     {
  105.     s.defaultReadObject();
  106.     timer = new Timer(100, new AutoScrollTimerAction());
  107.     }
  108. }
  109.